package cn.layku.web.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.IdUtil;
import cn.layku.web.service.member.MemberService;
import cn.layku.web.service.order.OrderService;
import cn.layku.web.util.ApiResult;
import com.alibaba.fastjson.JSON;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class ApiController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private OrderService orderService;

    @Resource
    private MemberService memberService;

    /**
     * 创建订单
     */
    @PostMapping("/order/create")
    @GlobalTransactional
    public Object orderCreate(@RequestParam Map<String, Object> map) {
        logger.info("xid->{}", RootContext.getXID());

        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        map.put("orderNo", snowflake.nextId());

        ApiResult<Object> orderAdd = orderService.add(map);
        logger.info("orderAdd->{}", JSON.toJSONString(orderAdd));

        ApiResult<Object> memberUpdate = memberService.update(map);
        logger.info("memberUpdate->{}", JSON.toJSONString(memberUpdate));

        return ApiResult.successResult();
    }


}